import java.util.Arrays;

/**
 * 希尔排序
 *
 * @Author: wanqian
 * @Date: 2021/2/1 12:04
 */
public class ShellSort {

    public static int[] arr = {4, 3, 65, 12, 1, -6, 46, 64};

    public static void main(String[] args) {
//        int[] arr = new int[1000000];
//        for (int i = 0; i < 1000000; i++) {
//            arr[i] = (int) (Math.random() * 1000);
//        }

        System.out.println("排序前" + Arrays.toString(arr));
        long start = System.currentTimeMillis();
        shellSort(arr);
        long end = System.currentTimeMillis();
        System.out.println("耗时" + (end - start) + "毫秒");
        System.out.println("排序后" + Arrays.toString(arr));
    }


    public static void shellSort(int[] arr) {
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                int value = arr[i];
                int index = i - gap;
                while (index >= 0 && arr[index] > value) {
                    arr[index + gap] = arr[index];
                    index -= gap;
                }
                arr[index + gap] = value;
            }
        }
    }
}

